package com.ydcycloud.sys.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.smart.mvc.dao.hibernate.Dao;
import com.smart.mvc.service.hibernate.impl.ServiceImpl;
import com.ydcycloud.company.model.Company;
import com.ydcycloud.sys.model.BasePersistentObject;
import com.ydcycloud.sys.model.PersistentObject;
import com.ydcycloud.sys.service.BaseService;

/**
 * 上层Service基类，实现了数据的CRUD
 * 
 * @param <DAO>
 * @param <T>
 * @param <ID>
 */
@SuppressWarnings("rawtypes")
public class BaseServiceImpl<DAO extends Dao, T extends PersistentObject, ID extends Serializable>
		extends ServiceImpl<DAO, T, ID> implements BaseService<DAO, T, ID> {
	
	/**
	 * 如果实体未持久化过则新建实体，否则更新实体
	 * 
	 * @param T t
	 * 
	 */
	public void save(T t) {
		if (t instanceof BasePersistentObject) {
			BasePersistentObject obj = (BasePersistentObject) t;
			Date date = new Date();
			if (obj.getId()==null) {
				obj.setCreateTime(date);
				obj.setLastUpdateTime(date);
				super.save(t);
			}else {
				obj.setLastUpdateTime(date);
				super.update(t);
			}
		}else {
			if (t.getId()==null) {
				super.save(t);
			}
			else {
				super.update(t);
			}
		}
	}
	
	/**
	 * Description:如果实体id为空   执行正常保存方法，如果id不为空也直接保存
	 * @author 唐海洋
	 * @Version 1.0 2016-5-24上午10:19:52 
	 * @param t
	 */
	public void saveDirect(T t) {
		
		if(t.getId() == null){
			save(t);
		}else{
			if (t instanceof BasePersistentObject) {
				BasePersistentObject obj = (BasePersistentObject) t;
				obj.setCreateTime(new Date());
				super.save(t);
			}else {
				super.save(t);
			}
		}
	}
}
